package com.xique.door.controller;


import com.xique.common.core.domain.R;
import com.xique.common.core.utils.SecurityUtils;
import com.xique.common.core.utils.StringUtils;
import com.xique.common.core.utils.poi.ExcelUtil;
import com.xique.common.core.web.controller.BaseController;
import com.xique.common.core.web.domain.AjaxResult;
import com.xique.common.core.web.page.TableDataInfo;
import com.xique.common.log.annotation.Log;
import com.xique.common.log.enums.BusinessType;
import com.xique.common.security.annotation.PreAuthorize;
import com.xique.door.api.request.build.RemoteHouseListRequest;
import com.xique.door.api.request.build.RemoteHouseUserListRequest;
import com.xique.door.bean.domain.BuildHouse;
import com.xique.door.bean.domain.User;
import com.xique.door.bean.request.HouseSelectUserRequest;
import com.xique.door.bean.request.buildHouse.BuildHouseAddRequest;
import com.xique.door.bean.request.buildHouse.BuildHouseBandUserRequest;
import com.xique.door.bean.request.buildHouse.BuildHouseDeleteUserRequest;
import com.xique.door.bean.request.buildHouse.BuildHouseSearchRequest;
import com.xique.door.bean.vo.SimpleHouseVo;
import com.xique.door.biz.service.IBuildHouseBizService;
import com.xique.door.biz.service.impl.BuildHouseBizService;
import com.xique.door.service.IBuildHouseService;
import com.xique.door.service.IUserService;
import com.xique.system.api.RemoteSystemUserService;
import com.xique.system.api.dto.OperateDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 房屋Controller
 *
 * @author caogq
 * @date 2021-09-09
 */
@RestController
@RequestMapping("/community/house")
public class BuildHouseController extends BaseController {

    @Autowired
    private IBuildHouseService buildHouseService;

    @Autowired
    private BuildHouseBizService buildHouseBizService;

    @Autowired
    private IUserService userService;

    @Autowired
    private IBuildHouseBizService houseBizService;

    @Autowired
    private RemoteSystemUserService remoteSystemUserService;

    /**
     * 查询房屋列表
     */
    @PreAuthorize(hasPermi = "door:house:list")
    @GetMapping("/conditionList")
    public TableDataInfo conditionList(BuildHouseSearchRequest request) {
        startPage();
        request.setParkId(SecurityUtils.getParkId());
        List<BuildHouse> list = buildHouseService.selectConditionBuildHouseList(request);
        List<String> userIds = list.stream().filter(e-> StringUtils.isNotEmpty(e.getUpdateBy())).map(e -> e.getUpdateBy()).collect(Collectors.toList());
        if (!userIds.isEmpty()){
            R<List<OperateDTO>> operateList = remoteSystemUserService.getOperateList(StringUtils.join(userIds, ","));
            List<OperateDTO> data = operateList.getData();
            list.forEach(item -> {
                OperateDTO operateDTO = data.stream().filter(e -> String.valueOf(e.getOperateId()).equals(item.getUpdateBy())).findAny().orElse(null);
                if (operateDTO!=null){
                    item.setUpdateBy(operateDTO.getOperateName());
                }
            });
        }
        return getDataTable(list);
    }

    /**
     * 查询房屋列表
     */
    @PreAuthorize(hasPermi = "door:house:list")
    @GetMapping("/list")
    public TableDataInfo list(BuildHouse buildHouse) {
//        startPage();
        buildHouse.setParkId(SecurityUtils.getParkId());
        List<BuildHouse> list = buildHouseService.selectBuildHouseList(buildHouse);
        return getDataTable(list);
    }

    @PreAuthorize(hasPermi = "door:house:list")
    @GetMapping("/areaBuildingHouse/list")
    public TableDataInfo areaBuildingHouseList(BuildHouseSearchRequest request) {
        startPage();
        request.setParkId(SecurityUtils.getParkId());
        List<SimpleHouseVo> list = buildHouseService.areaBuildingHouseList(request);
        return getDataTable(list);
    }

    /**
     * 新增保存房屋
     */
    @PreAuthorize(hasPermi = "door:house:add")
    @Log(title = "房屋", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult addSave(@RequestBody BuildHouseAddRequest request) {
        request.setParkId(SecurityUtils.getParkId());
        return buildHouseBizService.addHouse(request);
    }


    /**
     * 查询门禁用户信息列表
     */
    @GetMapping("/searchUser")
    public AjaxResult list(HouseSelectUserRequest request) {
        request.setParkId(SecurityUtils.getParkId());
        List<User> list = userService.houseSelectUsers(request);
        return AjaxResult.success(list);
    }

    /**
     * 修改保存房屋
     */
    @PreAuthorize(hasPermi = "door:house:edit")
    @Log(title = "房屋", businessType = BusinessType.UPDATE)
    @PutMapping("/edit")
    public AjaxResult editSave(@RequestBody  BuildHouse buildHouse) {
        return toAjax(buildHouseService.updateBuildHouse(buildHouse));
    }

    /**
     * 删除房屋
     */
    @PreAuthorize(hasPermi = "door:house:remove")
    @Log(title = "房屋", businessType = BusinessType.DELETE)
    @DeleteMapping("/remove")
    public AjaxResult remove(@RequestParam("ids") String ids) {
        return buildHouseService.deleteBuildHouseByIds(ids);
    }


    /**
     * 导出房屋列表
     */
    @PreAuthorize(hasPermi = "door:house:export")
    @PostMapping("/export")
    public void export(HttpServletResponse response, BuildHouseSearchRequest request) throws IOException {
        request.setParkId(SecurityUtils.getParkId());
        List<BuildHouse> list = buildHouseService.selectConditionBuildHouseList(request);
        ExcelUtil<BuildHouse> util = new ExcelUtil<BuildHouse>(BuildHouse.class);
        util.exportExcel(response, list, "house");
    }

    /**
     * 查询房下用户
     */
    @PreAuthorize(hasPermi = "door:house:userList")
    @GetMapping("/user/allocatedList")
    public TableDataInfo allocatedList(User user) {
        startPage();
        user.setParkId(SecurityUtils.getParkId());
        List<User> users = buildHouseBizService.selectAllocatedList(user);
        return getDataTable(users);
    }

    @PreAuthorize(hasPermi = "door:house:userList")
    @GetMapping("/user/unallocatedList")
    public TableDataInfo unallocatedList(User user) {
        startPage();
        user.setParkId(SecurityUtils.getParkId());
        List<User> users = buildHouseBizService.selectUnAllocatedList(user);
        return getDataTable(users);
    }

    @PreAuthorize(hasPermi = "door:house:userAdd")
    @PostMapping("/user/addAll")
    public AjaxResult addAllUser(@RequestBody BuildHouseBandUserRequest request) {
        request.setParkId(SecurityUtils.getParkId());
        return buildHouseBizService.houseAddAllUser(request);
    }

    @PreAuthorize(hasPermi = "door:house:userCancel")
    @Log(title = "单个取消授权", businessType = BusinessType.GRANT)
    @PostMapping("/user/cancel")
    public AjaxResult cancelLane(@RequestBody BuildHouseDeleteUserRequest request) {
        return buildHouseBizService.houseRemoveUser(request);
    }

    @PreAuthorize(hasPermi = "door:house:add")
    @Log(title = "智能生成", businessType = BusinessType.INSERT)
    @PostMapping("/batchAdd")
    public AjaxResult batchAdd(@RequestBody BuildHouseAddRequest request) {
        request.setParkId(SecurityUtils.getParkId());
        return buildHouseBizService.batchAdd(request);
    }

    @PostMapping("/openApi/v1/list")
    public AjaxResult houseListV1(@RequestBody RemoteHouseListRequest request) {
        return houseBizService.selectHouseList(request);
    }

    @PostMapping("/openApi/v1/userList")
    public AjaxResult houseUserListV1(@RequestBody RemoteHouseUserListRequest request) {
        return houseBizService.selectHouseUserList(request);
    }
}
